//
//  AppDelegate.m
//  TJSinaWeiBo_OC
//
//  Created by 唐后能 on 16/4/13.
//  Copyright © 2016年 itheima. All rights reserved.
//

#import "AppDelegate.h"
#import "TJMainViewController.h"
#import "TJWelcomeViewController.h"
#import "TJNewFeatureViewController.h"
#import "TJSaveDataTool.h"
#import "TJAccountViewModle.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // 0,设置全局皮肤.
    [self setAppearence];
    
    // 1,获取窗口.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    // 2,设置窗口的根控制器.
    self.window.rootViewController = [self getWindowRootVC];
//    self.window.rootViewController = [[TJWelcomeViewController alloc] init];
//    self.window.rootViewController = [[TJNewfeatureViewController alloc] init];
    
    // 3,显示主窗口.
    [self.window makeKeyAndVisible];
    
    return YES;
}


#pragma - 获取窗口根控制器.
- (UIViewController*)getWindowRootVC{

    // 判断用户是否登录.
    if (![TJAccountViewModle shareAccountViewModle].isLogin) { // 未登录
        return [[TJMainViewController alloc] init];
    }
    
    // 在判断是否是新版本.
    if ([self isNewVersion]) {
        return [[TJNewfeatureViewController alloc] init];
    }
    
    return [[TJWelcomeViewController alloc] init];;
}

#pragma - 判断是否是新版本.
-(BOOL)isNewVersion {

    // 1,获取本次应用的版本.
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleVersion"];
    
    // 2,获取沙盒中的版本.
    // 定义存取版本的key.
    NSString *versionKey = @"versionKey";
    NSString *lastVersion = [TJSaveDataTool valueForKey:versionKey];
    
    // 3,对比.
    if (![currentVersion isEqualToString:lastVersion]) { // 是新版本.
        // 保存新的版本号.
        [TJSaveDataTool setValue:currentVersion forKey:versionKey];
        
        return YES;
    }
    
    return NO;
}


#pragma - 设置全局的皮肤.
- (void)setAppearence {
    
    UIBarButtonItem *barBtnItem = [UIBarButtonItem appearance];
    [barBtnItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor]} forState:UIControlStateNormal];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
